package com.beater.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beater.controller.bean.LoginBean;
import com.beater.controller.bean.ResponseBodyJson;
import com.beater.controller.code.ResponseCode;
import com.beater.dao.beans.User;
import com.beater.global.auth0.TokenAutho;
import com.beater.service.IUserService;
import com.beater.utils.StringUtil;

@Controller
@RequestMapping("app")
public class LoginController {
	@Autowired
	IUserService userService;

	@RequestMapping("login")
	@ResponseBody
	public ResponseBodyJson<LoginBean> toLogin(
			String loginName,String pwd) {
		/*String loginName = params.get("loginName");
		String pwd = params.get("pwd");*/
		if (StringUtil.isNotEmpty(loginName) && StringUtil.isNotEmpty(pwd)) {
			User user = userService.getUser1(loginName, pwd);
			if (null != user) {
				String token = TokenAutho.createToken(user.getUid(),
						user.getUpassword());
				LoginBean bean = new LoginBean(user, token);
				return ResponseBodyJson.createLoginSuccessResBody( bean);
			}else{
				return new ResponseBodyJson( ResponseCode.ERROR_LOGIN.getCode(),null,ResponseCode.ERROR_LOGIN.getDesc());
			}

		}
		return ResponseBodyJson.createParamErrorResBody();

	}

	@RequestMapping("outLogin")
	@ResponseBody
	public ResponseBodyJson<LoginBean> outLogin() {
		
		return null ;
		

	}
}
